/*
 * meter.cpp
 *
 *  Created on: 2018-3-31
 *      Author: root
 */

#include "meter.h"
#include "protocol_meter.h"
#include "serial.h"
#include <unistd.h>
#include "app_debug.h"
#include <string.h>
#include "tools.h"
#include "share_type.h"
//uart_meter gmt0;
//uart_meter gmt1;
//uart_meter *ggp_mt = &gmt0;
//uart_meter *ggp_mt1 = &gmt1;
char _tmp_print_str[128];
MeterDataReal g_real_mtdata[MTNUM];
MT_OVER_COUNTER mt_over_counter[MTNUM];

MT_OVER_TIME mt_over_time[MTNUM];
u8 g_err_over_msg;//越限故障
//u8 flag_failReadMt[]={1,1};
//u32 time_failReadMt[]={0,0};
u8 notRead_Num[]={0,0,0,0,0,0,0,0,0};
uart_meter gp_mt;
u8 g_random_number=0;
static u8 Create645Frame(Meter_645* pmt, u8 *pflame, u16* lenFrame) {
	u8 cs = 0, i;
	u8 len = 0;
	memset(pflame,0xfe,4);
	len+=4;
	pflame[len++] = 0x68;
   memcpy(&pflame[len], (u8*)pmt->addr, 6);
	len += 6;
	pflame[len++] = 0x68;
	pflame[len++] = pmt->ctrl;
	pflame[len++] = pmt->lenData;
	for (i = 0; i < pmt->lenData; i++) {
		pflame[len++] = pmt->data[i] + 0x33;
	}
	for (i = 4; i < len; i++) {
		cs += pflame[i];
	}
	pflame[len++] = cs;   //? CS
	pflame[len++] = 0x16;
	*lenFrame = len;
	return 0;

}

u8 Analysis645Frame(Meter_645* pmt,u8* pBuf,u16 *lenBuf)
{
//	u16 len=*lenBuf;
	u8 i;
	if(memcmp(&pBuf[1],pmt->addr,6))
		return 1;
	pmt->ctrl=pBuf[8];
	pmt->lenData=pBuf[9];
  for(i=0;i<pmt->lenData;i++)
		   pBuf[10+i]-=0x33;
	if(memcmp(&pBuf[10],pmt->data,4))
	{
		debug_meter_mt("抄表数据项发生错误   data=%.*a   收到却是=%.*a",4,pmt->data,4,&pBuf[10]);
		return 2;
	}
	memcpy(pmt->data,&pBuf[10],pmt->lenData);
	return 0;

}
u8 Is645Frame(u8* pBuf, u16 *lenBuf, u16* needlen) {
	u16 len1 = *lenBuf, len2;
	u8 pos = 0;
	u8 cs1 = 0, cs2 = 0;
	u8 i;
	len2 = len1;
	loop: while (pBuf[pos] != 0x68 && len2 > 0) {
		pos++;
		len2 = len1 - pos;
	}
	if (len2 == 0) {
		*lenBuf = 12;
		len2 = 0;
		return 1;
	} else if (len2 < 12) {
		for (i = 0; i < len2; i++)
			pBuf[i] = pBuf[i + pos];
		*needlen = 12;
		*lenBuf = len2;
		return 2;
	}

	if (pBuf[pos + 7] != 0x68) {
		pos++;
		goto loop;
	}

	if (pBuf[pos + 8] != 0x91 && pBuf[pos + 8] != 0x94
			&& pBuf[pos + 8] != 0x93) {

		return 3;
	} else if (pBuf[pos + 8] == 0xd1)
	{
		printf("没有这个数据项  \r\n");
		return 0xff;
	}

	len2 = pBuf[pos + 9] + 12;
	if (len1 < len2) {

		for (i = 0; i < pos; i++)
			pBuf[i] = pBuf[i + pos];
		*needlen = len2;
		*lenBuf = len1 - pos;
		return 4;

	}
	cs1 = CountCS(&pBuf[pos], len2 - 2);
	cs2 = pBuf[pos + len2 - 2];
	if (cs1 != cs2)
	{
		printf("不大可能是校验位出错   cs1=%x  cs2=%x\r\n\r\n",cs1,cs2);
		return 5;
	}
	if (pBuf[pos + len2 - 1] != 0x16)
		return 6;
	*lenBuf = pBuf[pos + 9] + 12;
	*needlen = *lenBuf;
	return 0;
}
	u8 pframes_xxx[4][64];
//int getMtAddr(char* addr,uart_meter *gp_mt)
//{
//	u16 len=12;
//	u16 len1,lenframe,templen;
//	u8 i=0,l=0;
//	u8 gunid;
//	u8 *pframe1;
//	u8 pos=0;
//	u8 err;
//	if(gp_mt->comno==3)
//		gunid=0;
//	else
//		gunid=1;
//	pframe1=&pframes[gunid][0];
//
//
// 	u8 temp[]={0x68,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0x68,0x13,0x00,0xdf,0x16};
//	len1=WriteComPort(gunid,temp,len);
//	if(len1<len)
//	{
//		debug_meter_mt("发送字符串失败%d",len1);
//		return 1;
//	}
//	//sleep(1);
//	usleep(200000);//200ms
//	lenframe = 12;
//	for (; l < 10; l++) {
//		templen = ReadComPort(gunid,&pframe1[pos], lenframe);
//		if(templen==-1)
//			return 1;
//		pos += templen;
//		if (pos<12) {
//			debug_meter_mt("000实际接收到的645返回len=%d 帧=%.*a",templen,templen,pframe1);
//			lenframe = 12-pos;
//			continue;
//		}
//
//		lenframe = pos;
//		debug_meter_mt("111实际接收到的645返回len=%d 帧=%.*a  gunid=%d",lenframe,lenframe,pframe1,gunid);
//		err= Is645Frame(pframe1, &lenframe, &len);
//		if (err == 0)
//			break;
//		pos = lenframe;
//		if (pos<12) {
//			lenframe = 12-pos;
//			continue;
//		}
//		lenframe = len -pos;
//		if (err == 3 || err == 5 || err == 6)
//			break;
//
//	}
//	if (err != 0 || l>=10)
//	{
//		debug_meter_mt("抄取电表地址出错！！");//抄取电表地址失败
//		return 2;
//	}
//
//	debug_meter_mt("111抄取电表地址结果 :%.*a ",len,pframe1);
//	memcpy(addr,&pframe1[10],6);
//	for(i=0;i<6;i++)
//		addr[i]-=0x33;
//
//	gp_mt->sendErr(gunid,0);
//	debug_meter_mt("电表地址=%.*a  gunid=%d",6,addr,gunid);
//
//	return 0;
//
//
//}
	int getMtAddr(u8 portid,u8 gunid,char* addr)
	{
		u16 len=12;
		u16 len1,lenframe,templen;
		u8 i=0,l=0;
		u8 pframe1[32];
		u8 pos=0;
		u8 err;
	//	pframe1=&pframes_xxx[portid][0];

		printf("只有一个枪，抄取地址 portid=%d  gunid=%d \n",portid,gunid);
	 	u8 temp[]={0x68,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0x68,0x13,0x00,0xdf,0x16};
	 	debug_meter_mt("gunid=%d  抄取表地址包=%.*a",gunid,len,temp);
		len1=WriteComPort(portid,temp,len);
		if(len1<len)
		{
			debug_meter_mt("发送字符串失败%d",len1);
			return 1;
		}
		//sleep(1);
		usleep(200000);//200ms
		memset(pframe1,0,32);
		lenframe = 12;
		for (; l < 10; l++) {
			templen = ReadComPort(portid,&pframe1[pos], lenframe);
			if(templen==-1)
				return 1;
			pos += templen;
			if (pos<12) {
				debug_meter_mt("000实际接收到的645返回len=%d 帧=%.*a",templen,templen,pframe1);
				lenframe = 12-pos;
				continue;
			}

			lenframe = pos;
			debug_meter_mt("抄取地址 实际接收到的645返回len=%d 帧=%.*a  gunid=%d",lenframe,lenframe,pframe1,gunid);
			err= Is645Frame(pframe1, &lenframe, &len);
			if (err == 0)
				break;
			pos = lenframe;
			if (pos<12) {
				lenframe = 12-pos;
				continue;
			}
			lenframe = len -pos;
			if (err == 3 || err == 5 || err == 6)
				break;

		}
		if (err != 0 || l>=10)
		{
			debug_meter_mt("抄取电表地址出错！！");//抄取电表地址失败
			return 2;
		}

		debug_meter_mt("111抄取电表地址结果 :%.*a ",len,pframe1);
		memcpy(addr,&pframe1[10],6);
		for(i=0;i<6;i++)
			addr[i]-=0x33;

		gp_mt.sendErr(gunid,0);
		debug_meter_mt("电表地址=%.*a  gunid=%d",6,addr,gunid);

		return 0;


	}


int ReadMeterData(u8 port,u8 gunid,char* addr,char phase_num,int ct) {
	u8 err=0;

	u8 j = 0, l = 0, ret; //j数据项 l等待时间
    u8 succnum=0;
    u8 flag_succ=0;
	Meter_645 mt645;
	Meter_645 _mt645;
	MeterDataReal mtdata;
	static u32 kWh_last=0;
	static int errnum_readkwh[]={0,0,0,0,0,0,0,0};
	u16 lenframe, len,templen;
	//char phase_num=gp_mt.phase_num;
	char num=phase_num;
	u8 over_Val[9][3][3] = { { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }, { { 0,
			0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } } }; //表数量=2 ABC三相  过压0 欠压1 过流2
	u8 pos;
	u8 *pframe=&pframes_xxx[port][0];
	static u8 over_Val_last[9][3][3] = {
			{ { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }, { { 0, 0, 0 },
					{ 0, 0, 0 }, { 0, 0, 0 } } };

	u32 dataid_3[]=  { 0x0201ff00,   0x0202ff00, 0x02030000, 0x0000ff00 ,0x00010000,0x00000000};//
	u32 dataid_1[] = { 0x02010100, 0x02020100,0x02030000, 0x0000ff00 ,0x00010000,0x00000000};//0x02030100,645err err err
    u32 *dataid;


	u8 k = 0;
	if (over_Val[0][0][0] != 0)
		k = 1;
    if(num==3)
    {
    	debug_meter_mt("抄取三相表  抄取三相表  抄取三相表  !!!!!! gunid=%d port=%d",gunid,port);
    	dataid=&dataid_3[0];
    }
    else
    {
    	debug_meter_mt("抄取单相表  抄取单相表  抄取单相表  !!!!!!gunid=%d port=%d",gunid,port);
    	dataid=&dataid_1[0];
    }

    memset((u8*)&mtdata,0,sizeof(MeterDataReal));
	memcpy(&mt645.addr[0], addr, 6);
  //  debug_meter_mt("抄取第%d块表 gunid=%d 表号=%.*a",i,gunid,6,&mt645[i].addr[0]);
//    debug_meter_mt("111抄取第%d块表 gunid=%d 表号=%.*a",i,gunid,6,&mt645[i].addr[0]);
    succnum=0;
	for (j = 0; j < 6; j++) {
		notRead_Num[gunid]++;

	//	debug_meter_mt1("没能抄到表的次数达到 %d",notRead_Num[gunid]);
	    if(notRead_Num[gunid]>=5)
	    {
			printf("\r\n\r\n\r\n没能抄到表的次数达到  %d,电表故障\r\n",notRead_Num[gunid]);
			notRead_Num[gunid]=1;
				gp_mt.sendErr(gunid,1);

	    }
	//	printf("\r\n\r\n\r\n没能抄到表的次数达到  %d",notRead_Num);
		err = 0xee;
		mt645.ctrl = 0x11;
		mt645.lenData = 4;
		mt645.data[0] = (u8) dataid[j];  //
		mt645.data[1] = (u8) (dataid[j] >> 8);
		mt645.data[2] = (u8) (dataid[j] >> 16);
		mt645.data[3] = (u8) (dataid[j] >> 24);
		debug_meter_mt("组织抄表帧 !!!!!!!!!!!!!!!!!!! gunid=%d 数据项j=%d",gunid,j);
		Create645Frame(&mt645, pframe, &lenframe);
		WriteComPort(port,pframe, lenframe);
		debug_meter_mt("试着发送一包645 len=%d  gunid=%d %.*a",lenframe,gunid,lenframe,pframe);
		usleep(300000);

		memset(pframe, 0x00, 64);
		len = 12;
		pos = 0;
		l = 0;
		lenframe = 12;
		for (; l < 4; l++) {
			templen = ReadComPort(port,&pframe[pos], lenframe);
			if(templen==-1 || templen==0)
			{
				printf("没有抄到表 gunid=%d \r\n",gunid);
				debug_meter_mt("没有抄到表 表地址=%.*a",6,addr);
	//			sleep(1);
				usleep(10000);
				if(l>2)
					break;;
			}
			pos += templen;
			if (pos<12) {
				debug_meter_mt("实际接收到的645返回len=%d 帧=%.*a",templen,templen,pframe);
				lenframe = 12-pos;
				continue;
			}

			lenframe = pos;
			debug_meter_mt("实际接收到的645返回len=%d 帧=%.*a",lenframe,lenframe,pframe);
			err = Is645Frame(pframe, &lenframe, &len);
			if (err == 0)
			{
				break;
			}
			pos = lenframe;
			if (pos<12) {
				lenframe = 12-pos;
				continue;
			}
			lenframe = len -pos;
			if (err == 3 || err == 5 || err == 6)
				break;

		}
		if (err != 0)
		{
			printf("645err err err=%d notRead_Num[%d]=%d\r\n",err,gunid,notRead_Num[gunid]);
			continue;
		}

//		memcpy(pframe1, pframe, lenframe);
		_mt645 = mt645;
		debug_meter_mt("收到完整645包 %.*a",lenframe,pframe);
		ret = Analysis645Frame(&_mt645, pframe, &lenframe);

		if (ret != 0)
		{
			debug_meter_mt("222收到完整645包 %.*a ret=%d",lenframe,pframe,ret);
			continue;
		}
		if(notRead_Num[gunid]>1)
		    gp_mt.sendErr(gunid,0);
		notRead_Num[gunid]=0;


		if(j==1)
			debug_meter_mt("\r\n\r\n第%d块表 电流抄表包",lenframe,pframe);//块表 抄表结果  电压
		if (ret == 0) {
			pos = 4;
			switch (j) {
			case 0:
			    succnum|=1;
				mtdata.voltage[0]=0;
				mtdata.voltage[1]=0;
				mtdata.voltage[2]=0;
				if(_mt645.lenData==6)
					num=1;
				else
					num=phase_num;
//					mtdata[i].voltage=(bcdtouint(&_mt645.data[4], (u32)(_mt645.lenData-4)))/10;
				for (k = 0; k < num; k++) {
		//			printf("cccccccccccccccccccccccc k=%d  , g_phase_num=%d\r\n",k,g_phase_num);
					mtdata.voltage[k] =
							(bcdtouint(&_mt645.data[pos], 2));
					pos += 2;
					if (mtdata.voltage[k] > MaxV)  //过压
						over_Val[gunid][k][0] |= 1;
					else if (mtdata.voltage[k] < MinV)  //欠压
						over_Val[gunid][k][1] |= 1;
					if ((over_Val[gunid][k][0] & (1 << 0))
							!= (over_Val_last[gunid][k][0] & (1 << 0))) //与前一个值发生变化
							{

						if ((over_Val[gunid][k][0] & (1 << 0)) == 0) //过电压恢复
							mt_over_counter[gunid].Vover_counter[k] = 0;
						else if ((over_Val[gunid][k][0] & (1 << 0)) == 1) //过电压发生
							mt_over_counter[gunid].Vover_counter[k] = 1;

						over_Val_last[gunid][k][0] = over_Val[gunid][k][0];

					}
					if ((over_Val[gunid][k][1] & (1 << 0))
							!= (over_Val_last[gunid][k][1] & (1 << 0))) //与前一个值发生变化
							{
						if ((over_Val[gunid][k][1] & (1 << 0)) == 0) //欠电压恢复
							mt_over_counter[gunid].Vlow_counter[k] = 0;
						else if ((over_Val[gunid][k][1] & (1 << 0)) == 0) //欠电压发生
							mt_over_counter[gunid].Vlow_counter[k] = 1;
						over_Val_last[gunid][k][1] = over_Val[gunid][k][1];
					}
				}
			//	debug_meter("第%d块表 抄表结果  电压：%d",i,g_real_mtdata[i].voltage[0]);
				break;
			case 1:
			    succnum|=2;
			    float temp;
				mtdata.current[0]=0;
				mtdata.current[1]=0;
				mtdata.current[2]=0;
				if(_mt645.lenData==7)
					num=1;
				else
					num=phase_num;
//				mtdata[i].current=bcdtouint(&_mt645.data[4], _mt645.lenData-4);
				for (k = 0; k < num; k++) {

					mtdata.current[k] = bcdtosint(&_mt645.data[pos], 3);
					if(mtdata.current[k]<0)

						mtdata.current[k]=-mtdata.current[k];
                    temp=mtdata.current[k];
                    temp*=ct;
                    temp/=1000;
					mtdata.current[k]=temp;
					pos += 3;
					if (mtdata.current[k] > MaxI) //过流
						over_Val[gunid][k][2] |= 1;

					if ((over_Val[gunid][k][2] & (1 << 0))
							!= (over_Val_last[gunid][k][2] & (1 << 0))) //与前一个值发生变化
							{
						if ((over_Val[gunid][k][2] & (1 << 0)) == 0) //过流恢复
							mt_over_counter[gunid].Iover_counter[k] = 0;
						else if ((over_Val[gunid][k][2] & (1 << 0)) == 1) //过流发生
							mt_over_counter[gunid].Iover_counter[k] = 1;
						over_Val_last[gunid][k][2] = over_Val[gunid][k][2];
					}
				}
				debug_meter_mt1("第%d块表 抄表结果 电流:%d ",gunid,mtdata.current[gunid]);
				break;
			case 2: {
				float temp;

			    succnum|=4;
				mtdata.Power = bcdtosint(&_mt645.data[pos], 3);
				if (mtdata.Power < 0)
					mtdata.Power = -mtdata.Power;
				temp=mtdata.Power;
				temp*=ct;
				temp/=1000;
				mtdata.Power=temp;

				debug_meter_mt1("第%d块表 抄表结果 功率:%d ",gunid,mtdata.Power);

				pos += 3;
//							       mtdata[i].Pop[k][0]=_mt645.data[pos++];
//										 mtdata[i].Pop[k][1]=_mt645.data[pos++];
//										 mtdata[i].Pop[k][2]=_mt645.data[pos++];
			}
				break;

			case 3:

			{
				u32 kWh = 0;
				float fkWh_temp=0;
				errnum_readkwh[gunid]=0;
//				u8 flag = 0;

				for (k = 0; k < 5; k++) {

					mtdata.kWh[k] = bcdtouint(&_mt645.data[4 + 4 * k],
							4);
					if(k==0)
					  kWh = mtdata.kWh[k];
					fkWh_temp=mtdata.kWh[0];
					fkWh_temp*=ct;
					fkWh_temp/=1000;

					mtdata.kWh[k]=(u32)fkWh_temp;

			//		debug_meter_mt("第%d块表 抄表结果 电量:%d",i,mtdata.kWh[k]);//


				}
//				sprintf(_tmp_print_str,"第%d块表 抄表结果 原始电量:%d  计算电量=%d ct=%d",gunid,(int)kWh,(int)mtdata.kWh[0],ct);
//				gp_mt.writelog(_tmp_print_str);
				if(kWh_last!=0 && mtdata.kWh[0]==0)
				{
					debug_meter_mt1("抄表出现了严重问题  readmeter第%d块表 抄表结果 电量:%d",gunid,mtdata.kWh[0]);//检测计量故障结果
					debug_meter_mt1("%.*a",20,&_mt645.data[0]);
					sprintf(_tmp_print_str,"0x0000ff00 抄表出现了严重问题  readmeter第%d块表 抄表结果 电量:%d",gunid,(int)mtdata.kWh[0]);
					gp_mt.writelog(_tmp_print_str);

					break;
				}
				else
				{
//					sprintf(_tmp_print_str,"gunid=%d 0x0000ff00抄表结果 电量:%d",gunid,(int)mtdata.kWh[0]);
//					gp_mt.writelog(_tmp_print_str);
				}

			}
			succnum|=8;
			kWh_last=mtdata.kWh[0];
			j=6;
		//	debug_meter_mt("readmeter第%d块表 抄表结果 电量:%d",i,mtdata.kWh[0]);
				break;
			case 4:
			case 5:
			{
				u32 kWh = 0;
				float fkWh_temp=0;
				succnum|=8;
				errnum_readkwh[gunid]=0;;

				mtdata.kWh[0] = bcdtouint(&_mt645.data[4 ],4);
				kWh = mtdata.kWh[0];
				fkWh_temp=mtdata.kWh[0];
				fkWh_temp*=ct;
				fkWh_temp/=1000;

				mtdata.kWh[0]=(int)fkWh_temp;



//				sprintf(_tmp_print_str,"第%d块表 抄表结果 原始电量:%d  计算电量=%d ct=%d\r\n",gunid,(int)kWh,(int)mtdata.kWh[0],ct);
//				gp_mt.writelog(_tmp_print_str);
				if(kWh_last!=0 && mtdata.kWh[0]==0)
				{
					debug_meter_mt1("抄表出现了严重问题  readmeter第%d块表 抄表结果 电量:%d",gunid,mtdata.kWh[0]);
					sprintf(_tmp_print_str,",0x00010000或0x00000000 抄表出现了严重问题  readmeter第%d块表 抄表结果 电量:%d",gunid,(int)mtdata.kWh[0]);
					gp_mt.writelog(_tmp_print_str);

					break;
				}
				else
				{
//					sprintf(_tmp_print_str,"gunid=%d0x00010000或0x00000000 抄表结果 电量:%d",gunid,(int)mtdata.kWh[0]);
//					gp_mt.writelog(_tmp_print_str);

				}
//				for (k = 0; k < 5; k++) {
//
//					mtdata.kWh[k] = bcdtouint(&_mt645.data[4 + 4 * k],
//							4);
//					debug_meter_mt("第%d块表 抄表结果 电量:%d",i,mtdata.kWh[k]);
//					debug_meter_mt("%.*a",20,&_mt645.data[0]);
//				}
				for (k = 1; k < 5; k++) {
					mtdata.kWh[k] = 0;
				}

			}
		//	debug_meter_mt("readmeter第%d块表 抄表结果 电量:%d",i,mtdata.kWh[0]);
			j=6;
			succnum|=8;
			kWh_last=mtdata.kWh[0];
				break;
			default:
				break;
			}
		}
	}
	mtdata.read_flag=flag_succ;
	if(gunid==METER_IN)
	{
		if(succnum==0x0f)
		    flag_succ=1;
	}
	else
	{
		if((succnum&8)>0)
			 flag_succ=1;
		else if(succnum>0)
		{
			errnum_readkwh[gunid]++;
//			sprintf(_tmp_print_str,"gunid=%d抄到了其他数据项%x 但未能抄到电量次数=%d",gunid,succnum,(int)errnum_readkwh[gunid]);
//			gp_mt.writelog(_tmp_print_str);

			if(errnum_readkwh[gunid]>=5 && (errnum_readkwh[gunid]%5)==0)
			{
				sprintf(_tmp_print_str,"gunid=%d抄到了其他数据项%x 但未能抄到电量次数=%d,向主控报告故障",gunid,succnum,(int)errnum_readkwh[gunid]);
				notRead_Num[gunid]=1;
					gp_mt.sendErr(gunid,1);
			}
		}
	}
	if ( flag_succ==1) {
		mtdata.mtid = gunid;
		g_real_mtdata[gunid] = mtdata;
		g_real_mtdata[gunid].failReadMt = 0;
		debug_meter_mt1("抄到全部数据项  succnum=%d   gunid=%d",succnum,gunid);
		g_real_mtdata[gunid].read_flag=succnum;

//			if(gunnum==2)
//			{
//			    gp_mt->sendData(gunid, &g_real_mtdata[gunid]); //块表 抄表包
//			}
//			else if(gunnum==1)
//			{

				gp_mt.sendData(gunid, &g_real_mtdata[gunid]); //块表 抄表包
//		}
//		debug_meter_mt("\r\n\r\n第%d块表 枪号=%d  抄表结果  电压：%d  电流:%d  电量:%d\r\n", i,gunid,
//				g_real_mtdata[gunid].voltage[0], g_real_mtdata[gunid].current[0],
//				g_real_mtdata[gunid].kWh[0]);
	}
	else
		debug_meter_mt1("没有抄到全部数据项  succnum=%d   gunid=%d",succnum,gunid);

	return 0;
}
void Uart_Meter_Init(u8 portid,unsigned int baud)
{
    int ret;

	printf("正在试图打开串口%d   波特率=%d！！！！！\n",portid,baud);
	ret = OpenComPort(portid,baud, 8, "1", 'E');
    if(ret==-1)
    	printf("串口%d打开失败！！！！！\n",portid);
    else
    {
        printf("串口%d打开成功  波特率=%d！！！！！\n",portid,baud);
     }
}
//void Uart_Meter_Init(uart_meter *pmeter)
//{
//    int ret;
//    u8 gunid;
//	if(pmeter->comno==3)
//	{
//		gunid=0;
//		g_real_mtdata[0].failReadMt=0;//初始化设置电表抄读失败，防止未抄表成功就开始充电
//	}
//	else
//	{
//		gunid=1;
//		g_real_mtdata[1].failReadMt=0;
//	}
//	debug_meter_mt("正在试图打开串口%d   波特率=%d！！！！！",pmeter->comno,pmeter->baud);
//	ret = OpenComPort(gunid,pmeter->comno, pmeter->baud, 8, "1", 'E');
//    if(ret==-1)
//    	debug_meter_mt("串口%d打开失败！！！！！",pmeter->comno);
//    else
//    {
//    	debug_meter_mt("串口%d打开成功  波特率=%d！！！！！",pmeter->comno,pmeter->baud);
//     }
//}
void addtime_meter1000(int interval)//interval 为每次调用时的间隔
{
	g_random_number++;
	g_random_number%=g_random_number;
}
